#include "qbciscreencontrollershell.h"
#include <QCoreApplication>
#include <QEventLoop>
#include <iostream>

QBciScreenControllerShell::QBciScreenControllerShell()
    : terminalThread(new QThread),
      Controller(new QBciScreenController), Serial(new QBciSerial){

//  QString portName = "/dev/ttyUSB1";
    QString portName = "COM6";

  Serial->setPortname(portName);
  std::cout << "Open Serial:" + portName.toStdString() << std::endl;

  Serial->SerialInit();
  Controller->ScreenControllerInit();

  connect(Controller, &QBciScreenController::newConstruction, Serial,
          &QBciSerial::write);
  connect(this, &QBciScreenControllerShell::start, Controller,
          &QBciScreenController::setAllBlock);
  connect(this, &QBciScreenControllerShell::stop, Controller,
          &QBciScreenController::resetAllBlock);

  terminal = new QBciTerminal(terminalThread);

  connect(terminal, &QBciTerminal::start, this,
          &QBciScreenControllerShell::startSlot);
  connect(terminal, &QBciTerminal::stop, this,
          &QBciScreenControllerShell::stopSlot);
  connect(terminal, &QBciTerminal::quit, this,
          &QBciScreenControllerShell::quitSlot);

  startAllThread();

}

QBciScreenControllerShell::~QBciScreenControllerShell() {
  stopAllThread();
}

void QBciScreenControllerShell::stylePaint(QPainter *painter){

}

void QBciScreenControllerShell::setWaveType(QBciRender::WaveType wave) {
    Controller->setWaveType(wave);
}

void QBciScreenControllerShell::setEnableWave(const bool &flag, const int &ID){
    if(flag){
        Controller->setBlockFlick(ID, flag);
    }
    else{
        Controller->setBlockFlick(ID, flag);
    }
}

void QBciScreenControllerShell::setFreq(const int &ID, const double &freq) {
    Controller->setBlockFreq(ID, freq);
}

void QBciScreenControllerShell::startFlick() {
    emit start();
}

void QBciScreenControllerShell::stopFlick() {
    emit stop();
}

void QBciScreenControllerShell::startSlot() { emit start(); }

void QBciScreenControllerShell::stopSlot() { emit stop(); }

void QBciScreenControllerShell::quitSlot() {
  stopAllThread();
  QCoreApplication::instance()->quit();
}

void QBciScreenControllerShell::startAllThread() {
    terminalThread->start();
    Serial->serialThread->start();
    Controller->screenControllerThread->start();
}

void QBciScreenControllerShell::stopAllThread() {
    Serial->serialThread->quit();
    Controller->screenControllerThread->quit();
    Serial->serialThread->wait();
    Controller->screenControllerThread->wait();
}

QBciTerminal::QBciTerminal(QThread *thread) : QObject() {
  moveToThread(thread);
  connect(thread, &QThread::started, this, &QBciTerminal::run);
  connect(thread, &QThread::finished, this, &QObject::deleteLater);
}

QBciTerminal::~QBciTerminal() { emit quit(); }

void QBciTerminal::run() {
  QLogger::Info("BciScreenController Application Terminal");
  QString command;
  QTextStream in(stdin);
  while (true) {
    in >> command;
    if (command == "help") {
      QLogger::Info("QBciApplication Terminal:");
      QLogger::Info("start		:start Application");
      QLogger::Info("stop       :stop flick");
      QLogger::Info("quit		:quit Application");
    } else if (command == "start")
      emit start();
    else if (command == "stop")
      emit stop();
    else if (command == "quit") {
      emit quit();
      break;
    }
//    else
//      QLogger::Info("Unrecognized Command, typing \"help\" to find more");
  }
  this->deleteLater();
}
